package com.qf.controller;



import com.github.pagehelper.PageInfo;
import com.qf.pojo.Department;
import com.qf.pojo.Employee;
import com.qf.pojo.EmployeeVo;
import com.qf.service.DepartmentService;
import com.qf.service.EmployeeService;
import com.qf.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(path = "admin/emp/")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentService departmentService;


    @RequestMapping(path = "listjson")
    @ResponseBody
    public JsonResult listJson(){
        JsonResult jsonResult = new JsonResult();
        List<EmployeeVo> empsAndDept = employeeService.getEmpsAndDept();
        jsonResult.setData(empsAndDept);
        return jsonResult;
    }


    @GetMapping(path = "dodeljson/{empId}")
    @ResponseBody
    public JsonResult doDelJson(@PathVariable("empId") Integer empId){
        JsonResult jsonResult = new JsonResult();
        System.out.println("empId"+empId);
        int i = employeeService.dropEmp(empId);
        if (i>0){
            jsonResult.setData(i);
        }else {
            jsonResult.setErrorCode(500);
            jsonResult.setErrorMsg("删除失败");
        }
        return jsonResult;
    }

    @GetMapping(path = "getdepts")
    @ResponseBody
    public JsonResult getDepts(){
        JsonResult jsonResult = new JsonResult();
        List<Department> allDept = departmentService.getAllDept();
        jsonResult.setData(allDept);
        return jsonResult;
    }
    @GetMapping("doaddjson")
    @ResponseBody
    public JsonResult doAddJson(EmployeeVo employeevo){
        System.out.println("employee==>"+employeevo);
        JsonResult jsonResult = new JsonResult();
        int result = employeeService.addEmp(employeevo);
        System.out.println(result);
        if (result >0){
            jsonResult.setData(result);
        }else if (result == -1){
            jsonResult.setErrorCode(500);
            jsonResult.setErrorMsg("添加失败");
        }
        return jsonResult;
    }

    @GetMapping(path = "doeditjson")
    @ResponseBody
    public JsonResult doEdit(EmployeeVo employee){
        JsonResult jsonResult =new JsonResult();
        System.out.println("employee----------->"+employee);
        int i = employeeService.editEmp(employee);
        System.out.println(i);
        if (i>0){
            jsonResult.setData(i);
        }else {
            jsonResult.setErrorCode(500);
            jsonResult.setErrorMsg("修改失败");
        }
        return jsonResult;
    }


    @GetMapping(path = "toeditjson/{empId}")
    @ResponseBody
    public JsonResult toEditJson(@PathVariable("empId") Integer empId){
        JsonResult jsonResult = new JsonResult();
        Employee emp = employeeService.getEmpAndDeptByEmpId(empId);
        System.out.println(emp);
        if (emp != null){
            jsonResult.setData(emp);
        }else {
            jsonResult.setErrorCode(500);
            jsonResult.setErrorMsg("用户不存在");
        }
        return jsonResult;
    }
    @GetMapping(path="getempsbypage/{pageNow2}/{pageSize}")
    @ResponseBody
    public JsonResult getEmpsByPage(@PathVariable("pageNow2") Integer pageNow, @PathVariable("pageSize") Integer pageSize){
        PageInfo<EmployeeVo> emps = employeeService.getEmpsAndDeptByPage(pageNow,pageSize);
        System.out.println("emps.getList()"+emps.getList());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(emps);
        return jsonResult;
    }


    @RequestMapping(path = "todellist")
    @ResponseBody
    public JsonResult doDelList(@RequestBody List<Integer> checkList){
        System.out.println("checkList------------>"+checkList.toString());
        JsonResult jsonResult = new JsonResult();
        int num = 0;
        for (Integer id : checkList) {
            int i = employeeService.dropEmp(id);
            if (i>0){
                num++;
            }
        }
        if (num >= checkList.size()){
            jsonResult.setData(num);
        }else {
            jsonResult.setErrorCode(500);
            jsonResult.setErrorMsg("删除失败");
        }
        return jsonResult;
    }

}
